//
//  DYNavigationCOntroller.m
//  DYLYJ
//
//  Created by JM on 15/10/28.
//  Copyright © 2015年 JM. All rights reserved.
//

#import "DYNavigationController.h"
#import "UIColor+DY.h"
#import "UIBarButtonItem+DY.h"
#import <objc/runtime.h>
#import "JRSwizzle.h"
@implementation DYNavigationController
#pragma mark 一个类只会调用一次
+ (void)initialize
{
    // 1.取出设置主题的对象
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[DYNavigationController class], nil];
    //[navBar setBackgroundImage:[UIImage imageNamed:@"navibar"] forBarMetrics:UIBarMetricsDefault];
    navBar.barTintColor = [UIColor greenColor];//backgroundclolor
    //title字体 颜色 大小
    NSDictionary *dict = @{
                           NSForegroundColorAttributeName:[UIColor whiteColor],
                           NSFontAttributeName:[UIFont systemFontOfSize:24 weight:bold]};
    [navBar setTitleTextAttributes:dict];

    
//    //修改导航条上的UIBarButtonItem
//    UIBarButtonItem *appearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];
//
//    //设置导航栏的字体包括backBarButton和leftBarButton，rightBarButton的字体
//    NSDictionary *textAttributes = @{UITextAttributeFont: [UIFont systemFontOfSize:18],
//                                     UITextAttributeTextColor: [UIColor blueColor],
//                                     UITextAttributeTextShadowColor: [UIColor whiteColor],
//                                     UITextAttributeTextShadowOffset: [NSValue valueWithCGSize:CGSizeMake(1, 1)]};
//    
//    [appearance setTitleTextAttributes:textAttributes forState:1];//forState为0时为下正常状态，为1时为点击状态。
//    
    
//    //修改leftBarButton，rightBarButton背景效果
//    [appearance setBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
//                          forState:UIControlStateNormal
//                             style:UIBarButtonItemStyleBordered
//                        barMetrics:UIBarMetricsDefault];
//
//    
//    //backBarButton需要单独设置背景效果。只能在ios6.0以后才能用
//    [appearance setBackButtonBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
//                                    forState:UIControlStateNormal
//                                  barMetrics:UIBarMetricsDefault];
//    
//    [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake( -100, -100)
//                                       forBarMetrics:UIBarMetricsDefault];
//    [appearance setBackButtonBackgroundVerticalPositionAdjustment:-12 forBarMetrics:UIBarMetricsDefault];

}

//#pragma mark 控制状态栏的样式
///*
// 状态栏的管理：
// 1> iOS7之前：UIApplication
// 2> iOS7开始：交给对应的控制器去管理
// */
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    // 白色样式
//    return UIStatusBarStyleLightContent;
//}
//
//- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
//{
//    if (self.viewControllers.count) {
//        viewController.hidesBottomBarWhenPushed = YES;
//    }
//    [super pushViewController:viewController animated:animated];
//}

//- (instancetype)initWithCoder:(NSCoder *)coder
//{
//    self = [super initWithCoder:coder];
//    if (self) {
//        <#statements#>
//    }
//    return self;
//}

- (void)changeNaviBarPushedViewController:(UIViewController *)vc {
    
    vc.view.backgroundColor = [UIColor redColor];
    
    if (!vc.navigationItem.leftBarButtonItem && (self.viewControllers.firstObject != nil) && (self.viewControllers.firstObject != vc)) {
        vc.navigationItem.leftBarButtonItem = [UIBarButtonItem backBarItemWithTarget:self action:@selector(clickBackButton:)];
        
    }
    [self.navigationBar setTitleVerticalPositionAdjustment:-(kDYBarDefaultHeight-44)/2.0 forBarMetrics:UIBarMetricsDefault];
}


- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self changeNaviBarPushedViewController:viewController];
    [super pushViewController:viewController animated:animated];
}

- (void)viewWillAppear:(BOOL)animated {
   //push 不会调用此函数。
    [self changeNaviBarPushedViewController:self.topViewController];
    [super viewWillAppear:animated];
    
}



- (void)clickBackButton:(id)sender {
    [self popViewControllerAnimated:YES];
}
@end

@implementation UINavigationBar (changeHeigh)
static char customHeightKey;
static char customWidthKey;

- (instancetype)JM_initWithFrame:(CGRect)frame
{
    UINavigationBar *myself = [self JM_initWithFrame:frame];
    myself.customHeight = kDYBarDefaultHeight;
    myself.customWidth = [UIScreen mainScreen].bounds.size.width;
    return myself;
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGSize newSize = CGSizeMake(self.customWidth,self.customHeight);
    return newSize;
}

- (NSUInteger)customHeight {
    NSNumber * number = objc_getAssociatedObject(self, &customHeightKey);
    return number.unsignedIntegerValue;
}

- (void)setCustomHeight:(NSUInteger)customHeight {
    NSNumber * number = @(customHeight);
    objc_setAssociatedObject(self, &customHeightKey, number, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSUInteger)customWidth {
    NSNumber * number = objc_getAssociatedObject(self, &customWidthKey);
    return number.unsignedIntegerValue;
}

- (void)setCustomWidth:(NSUInteger)customWidth {
    NSNumber * number = @(customWidth);
    objc_setAssociatedObject(self, &customWidthKey, number, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError *error = nil;
        [UINavigationBar jr_swizzleMethod:@selector(initWithFrame:) withMethod:@selector(JM_initWithFrame:) error:&error];
        NSAssert(!error, [error description]);
    });
}

@end